var dot_inter,bool;
var $ = getApp(), methods = require("../methods.js");
var windowWidth = wx.getSystemInfoSync().windowWidth
Page(Object.assign({}, methods, {
	data:{
		speed: 10,/**转盘速度 */
		speedDot: 1000,/**点切换速度 */
		dotColor: ['#ffffff', '#FCDF00'],
		dotColor_1: ['#ffffff', '#FCDF00'],
		dotColor_2: ['#FCDF00', '#ffffff'],
		jxshow:false,
		showmaskrule:false
	},
	downloadFile:function(jxarr,i){
		var that = this
		if(jxarr[i].pic){
			wx.downloadFile({
				url: jxarr[i].pic,
				success: function (res) {
					if(res.tempFilePath) {
						jxarr[i].pic = res.tempFilePath;
						that.setData({jxarr:jxarr})
					}
				},
				complete:function(){
					if(jxarr.length > i+1){
						that.downloadFile(jxarr,i+1);
					}else{
						that.dotStart();
					}
				}
			})
		}else{
				if(jxarr.length > i+1){
					that.downloadFile(jxarr,i+1);
				}else{
					that.dotStart();
				}
		}
	},
	_onLoad: function () {
		var that = this;
		var id = '';
		if (that.options && that.options.id) {
			id = that.options.id
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
			}
		}
		$.get('xydzp/index',{id:id},function(res){
			that.setData(res)
			wx.setNavigationBarTitle({
				title: res.info.name
			});
			that.downloadFile(res.jxarr,0);
			if(that.data.info.fanwei==1){
				wx.getLocation({
					type: 'gcj02',
					success:function(res){
						var latitude = res.latitude
						var longitude = res.longitude
						that.setData({latitude:latitude,longitude:longitude})
						console.log(longitude)
					}
				})
			}
		})
	},
	changemaskrule:function(){
		this.setData({showmaskrule:!this.data.showmaskrule})
	},
	changemask:function(){
		this.setData({jxshow:!this.data.jxshow})
		this.reLoad();
	},
	rollStart:function(){
		var that=this;
		if (bool) return; // 如果在执行就退出
		bool = true; // 标志为 在执行
		$.post('xydzp/index',{id:that.data.info.id,op:'getjx',longitude:that.data.longitude,latitude:that.data.latitude},function(res){
			if(res.status!=1){
				$.alert(res.msg);
				bool = false;
				return;
			}else{
				//奖品数量等于10,指针落在对应奖品区域的中心角度[252, 216, 180, 144, 108, 72, 36, 360, 324, 288]
				var angel = 360 - 360 / res.jxcount * res.jxindex;
			}
			angel += 360*6;
			var baseStep = 30
			// 起始滚动速度
			var baseSpeed = 0.3
			var count = 1;

			var timer = setInterval(function () {
				that.setData({
					angelTo:count
				})
				clearInterval(dot_inter);
				that.drawCanvas();
				if (count == angel) {
					console.log('完毕');
					bool = false;
					clearInterval(timer)
					that.setData({jxshow:true,jxmc:res.jxmc,jx:res.jx});
					that.dotStart();
					if(res.jxtp == 2 && res.spdata){
						wx.sendBizRedPacket({
							timeStamp: res.spdata.timeStamp, // 支付签名时间戳，
							nonceStr: res.spdata.nonceStr, // 支付签名随机串，不长于 32 位
							package: res.spdata.package, //扩展字段，由商户传入
							signType: res.spdata.signType, // 签名方式，
							paySign: res.spdata.paySign, // 支付签名
							success:function(res){
								console.log(res);
							},
							fail:function(res){
								console.log(res);
							},
							complete:function(res){
								console.log(res);
							}
						})
					}
				}
				count = count + baseStep * (((angel - count) / angel) > baseSpeed ? baseSpeed : ((angel - count) / angel))
				if (angel - count < 0.5) {
					count = angel
				}
			}, that.data.speed)
		});
	},
	onShareAppMessage: function () {
		var that = this
		var thisurl = $._fullurl();
		if(this.options.scene){
			var scene = this.options.scene
			var scenes = scene.split('_');
			thisurl += '?scene=' + $.mid + (scenes[1] ? '_' + scenes[1] :'') + (scenes[2] ? '_' + scenes[2] :'');
		}else{
			if (thisurl.indexOf('?') > 0) {
				thisurl += '&scene=' + $.mid
			} else {
				thisurl += '?scene=' + $.mid
			}
		}
		//console.log(thisurl);
		var title = that.data.info.name;
		if(that.data.info.sharetitle){
			title = that.data.info.sharetitle;
		}
		$.post("xydzp/share",{ hid:that.data.info.id },function(res){
			if(res.status==1){
				setTimeout(function(){
					that.reLoad()
				},1000);
			}else if(res.status==0){
				//dialog(res.msg);
			}
		});
		return {
			title: title,
			imageUrl: that.data.info.sharepic ? that.data.info.sharepic:'',
			path: thisurl
		}
	},
	drawCanvas: function (){
		var that = this;
		var ctx = wx.createCanvasContext('roulette', this);
		var angelTo = this.data.angelTo || 0;
		var width = windowWidth/750*650;
		var height = width;
		var x = width / 2;
		var y = width / 2;
		var num = that.data.jxarr.length;
		ctx.translate(x, y)
		ctx.clearRect(-width, -height, width, height);

		ctx.rotate(angelTo * Math.PI / 180);
		// 画外圆
		ctx.beginPath();
		ctx.lineWidth = width/2;
		ctx.strokeStyle = '#FFF7C5';
		ctx.arc(0, 0, width/4, 0, 2 * Math.PI)
		ctx.stroke();

		ctx.beginPath();
		ctx.lineWidth = 1;
		ctx.strokeStyle = '#D9644F';
		ctx.arc(0, 0, width/2-1, 0, 2 * Math.PI)
		ctx.stroke();

		ctx.beginPath();
		ctx.lineWidth = 2;
		ctx.strokeStyle = '#FDF28C';
		ctx.arc(0, 0, width/2-3, 0, 2 * Math.PI)
		ctx.stroke();

		ctx.beginPath();
		ctx.lineWidth = 15;
		ctx.strokeStyle = '#F8645E';
		ctx.arc(0, 0, width/2-14, 0, 2 * Math.PI)
		ctx.stroke();
		// 装饰点
		var dotColor = that.data.dotColor;
		var startAngel = 0;
		for (var i = 0; i < 26; i++) {
			ctx.beginPath();
			var radius = width/2-14;
			var xr = radius * Math.cos(startAngel)
			var yr = radius * Math.sin(startAngel)
			ctx.fillStyle = dotColor[i % dotColor.length]
			ctx.arc(xr, yr, 4, 0, 2 * Math.PI)
			ctx.fill()
			startAngel += 360 / 26 * (Math.PI / 180);
		}
		
		var jxarr = that.data.jxarr;
		ctx.rotate(-(360/num) * Math.PI / 180);
		for (var i = 0; i < num; i++) {
			ctx.rotate((360/num) * Math.PI / 180);
			//ctx.setFontSize(14)
			ctx.font = 'normal bold 14px Arial';
			ctx.fillStyle = '#e75228',
			ctx.textAlign = "center";
			ctx.fillText(jxarr[i].mc, 0,-(width/2-50));
			
			ctx.drawImage(jxarr[i].pic,-20,-(width/2-70),40,40)
			//ctx.restore();
		}
		if(num%2 == 0){
			ctx.rotate((180/num) * Math.PI / 180);
		}
		for(var i = 0; i < num; i++){
			//ctx.save();
			ctx.rotate((360/num) * Math.PI / 180);
			ctx.beginPath()
			ctx.lineWidth = 2;
			ctx.moveTo(0, 0)
			ctx.lineTo(0, width/2-20)
			ctx.setStrokeStyle('#f6625c')
			ctx.stroke()
		}
		ctx.draw()
	},
	dotStart: function () {
		var that = this;
		var times = 0;
		that.drawCanvas();
		dot_inter = setInterval(function () {
			if (times % 2) {
				var dotColor = that.data.dotColor_1
			} else {
				var dotColor = that.data.dotColor_2
			}
			times++;
			that.setData({
				dotColor: dotColor
			})
			that.drawCanvas();
		}, that.data.speedDot)
	}
}))